﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace NetLogger
{
	public class NetLoggerStatus : NetPropertyBase
	{
		private const double UpdateIntervalBuffer = 10d;

		private DateTimeOffset _recordTime;
		private double _updateInterval;

		public NetLoggerStatus( XElement node )
		{
			Update( node );
		}

		public string LoggerId
		{
			get;
			private set;
		}

		public bool IsCurrent
		{
			get
			{
				return ( NetActivation.Instance.RecordTime - _recordTime ).TotalSeconds <= _updateInterval + UpdateIntervalBuffer;
			}
		}

		public void Update( XElement node )
		{
			foreach ( XElement item in node.Elements() )
			{
				switch ( item.Name.LocalName )
				{
					case "logger_id":
						LoggerId = NetPost.DecodedString( item );
						break;
					case "record_time":
						_recordTime = NetPost.FromMySQLDate( item );
						break;
					case "update_interval":
						_updateInterval = ( double ) item;
						break;
				}
			}

			OnPropertyChanged( "IsCurrent" );
		}

		public override string ToString()
		{
			return LoggerId;
		}

		public override int GetHashCode()
		{
			return LoggerId.GetHashCode();
		}
	}
}
